/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2009 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2ImageOpenGL.h 131 2009-06-05 05:58:05Z jlauha $
 * $Revision: 131 $
 */

#ifndef T2IMAGEOPENGL_H_INCLUDED
#define T2IMAGEOPENGL_H_INCLUDED


namespace turska
{
    //! Implementation of Turska Image which uses OpenGL.
    /*! \note In this implementation the IF_COLOR and IF_ALPHA flags
     *        are always assumed to be enabled.
     */
    class ImageOpenGL : public turska::Image
    {
    public:

        //! Constructs a new ImageOpenGL from given texture and U,V coordinates.
        /*!
         * \param width original width of the image in pixels.
         * \param height original height of the image in pixels.
         * \param flags initial flags to use for the image.
         * \param glId OpenGL texture name for the texture image,
         *             ownership is transferred.
         * \param uv top-left and bottom-right U,V coordinates in
         *           texture (4 floats).
         */
        ImageOpenGL(UI32 width, UI32 height, UI32 flags, UI32 glId, const F32 *uv);
        //! Destructor deletes the texture.
        virtual ~ImageOpenGL();

        // From Image:

        virtual void setColor(const Color &color) { mColor = color; }

        virtual void setTargetSize(REAL width, REAL height) { mTargetWidth = width; mTargetHeight = height; mUsingExplicitTargetSize = true; }
        virtual void resetTargetSize() { mTargetWidth = (F32)mWidth; mTargetHeight = (F32)mHeight; mUsingExplicitTargetSize = false; }

        virtual void setAngle(REAL angle) { mAngle = angle; }

        virtual void setScale(REAL scale) { mScaleX = mScaleY = scale; }
        virtual void setScale(REAL scaleX, REAL scaleY) { mScaleX = scaleX; mScaleY = scaleY; }

        virtual void draw(REAL x, REAL y) { draw(mDrawMode, mFlags, x, y); }
        virtual void draw(IMAGE_DRAW_MODE drawMode, UI32 flags, REAL x, REAL y, const Rectangle *sourceRect = 0) { draw(drawMode, flags, x, y, mColor, sourceRect); }
        virtual void draw(IMAGE_DRAW_MODE drawMode, UI32 flags, REAL x, REAL y, const Color &color, const Rectangle *sourceRect = 0) { draw(drawMode, flags, x, y, color.c, sourceRect); }

        // ImageOpenGL additions:

        inline UI32 getOpenGLId() const { return mGLId; }
        inline F32 * getUV() { return mUV; }

    protected:

        void draw(IMAGE_DRAW_MODE drawMode, UI32 flags, F32 x, F32 y, const UI8 *color, const Rectangle *sourceRect);

        UI32 mGLId;
        F32 mUV[4];
        const F32 mHalfWidth;
        const F32 mHalfHeight;
        Color mColor;
        F32 mTargetWidth;
        F32 mTargetHeight;
        F32 mAngle;
        F32 mScaleX, mScaleY;
        bool mUsingExplicitTargetSize;
    }; // ImageOpenGL
} // namespace turska


#endif // !T2IMAGEOPENGL_H_INCLUDED
